import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:yt_devops/components/app_permission/auth_tab.dart';
import 'package:yt_devops/components/app_permission/yt_tab_view_model.dart';
import 'package:yt_devops/components/log_utils.dart';

import 'package:yt_devops/components/time_range_picker.dart';
import 'package:yt_devops/components/yt_bottom_picker.dart';
import 'package:yt_devops/pages/devices/device_list/components/filter_site_tags.dart';
import 'package:yt_devops/pages/devices/device_list/components/filter_status_tags.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/site_mains_elec_detail/keep_aliver_wrapper.dart';
import 'package:yt_devops/pages/site/site_observer_list/views/my_top_tabbar.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/services/task/task_services.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/debounce_utils.dart';
import 'package:yt_devops/utils/enums.dart';
import 'list_page_view.dart';
import 'task_list_controller.dart';

class TaskListPage extends GetView<TaskListController> {
  TaskListPage({Key? key}) : super(key: key);

  TaskServices taskServices = Get.find<TaskServices>();

  @override
  Widget build(BuildContext context) {
    Log.d('TaskListPage====context');
    return Scaffold(
        // appBar: AppBar(
        //   title: Text(controller.title),
        //
        // ),
        body: SafeArea(
      child: AuthTab(
        tabList: controller.tabs,
        build: (context, filtes, tabController) {
          controller.updateTaskType(tabController.index);
          return Column(
            children: [
              Column(children: [
                DecoratedBox(
                  decoration: const BoxDecoration(color: Colour.white),
                  child: Row(
                    children: [
                      Expanded(
                          child: BrnSearchText(
                        maxHeight: 54,
                        innerColor: Colour.backgroundColor,
                        innerPadding: const EdgeInsets.fromLTRB(16, 8, 15, 8),
                        hintStyle: TextStyles.lf16,
                        textStyle: TextStyles.f16,
                        textInputType: TextInputType.number,
                        searchController: controller.searchController,
                        controller: controller.textController,
                        hintText: ResKey.workOrderSearchHint.tr,
                        onTextCommit: (content) => controller.commit(content),
                        onTextClear: () {
                          controller.commit('');
                          return false;
                        },
                        onTextChange: DebounceUtils.debounceInput(
                            (p0) => controller.commit(p0), 400),
                      )),
                      InkWell(
                        onTap: () => controller.toFilter(context),
                        child: Padding(
                          padding: EdgeInsets.fromLTRB(5.w, 0, 17.w, 0),
                          child: Image.asset(
                            ImageAssets.iconFilter,
                            width: 24.w,
                            height: 24.w,
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                Container(
                  padding: const EdgeInsets.fromLTRB(4, 0, 4, 0),
                  color: Colour.white,
                  child: GetBuilder(
                    init: controller,
                    builder: (controller) {
                      return BrnTabBar(
                        controller: tabController,
                        isScroll: false,
                        labelStyle: TextStyle(
                            fontSize: 16.sp, fontWeight: FontWeight.bold),
                        unselectedLabelStyle: TextStyle(fontSize: 16.sp),
                        labelColor: Colour.primaryColor,
                        unselectedLabelColor: Colour.black,
                        indicatorPadding:
                            EdgeInsets.symmetric(horizontal: 20.w),
                        indicatorWeight: 3.0.h,
                        tabs: filterTabs(filtes),
                      );
                    },
                  ),
                ),
                Container(
                  height: 0.5,
                  color: Colour.dividerColor,
                )
              ]),
              Expanded(
                child: TabBarView(
                  controller: tabController,
                  children: filtes
                      .map((e) => KeepAliveWrapper(e.permissionWidget))
                      .toList(),
                ),
              )
            ],
          );
        },
      ),
    ));
  }

  List<BadgeTab> filterTabs(List<YtTabViewModel> tabs) {
    return tabs.map((item) {
      return BadgeTab(
        text: item.labelString,
      );
    }).toList();
  }
}
